package com.lyy.photovoltaic.service.impl;

import com.lyy.photovoltaic.beans.Staffs;
import com.lyy.photovoltaic.common.utils.MyMD5Util;
import com.lyy.photovoltaic.common.vo.ResultCode;
import com.lyy.photovoltaic.common.vo.ResultVO;
import com.lyy.photovoltaic.mapper.StaffsMapper;
import com.lyy.photovoltaic.service.StaffsService;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

@Service
public class StaffsServiceImpl implements StaffsService {
    @Autowired
    StaffsMapper staffsMapper;
    @Override
    public ResultVO staffsLogin(String sId, String pwd) {
        boolean flag = false;
        //1.根据账号名查询用户信息
        Staffs staff = staffsMapper.selectByPrimaryKey(sId);
        //2.判断
        if (staff != null) {
            try {
                flag = MyMD5Util.validPassword(pwd, staff.getStaffPassword());
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (flag) {
                //如果登录验证成功，则需要生成token
                //使用jwt生成token字符串
                JwtBuilder builder = Jwts.builder();
                String token = builder.setSubject(staff.getStaffName())        //jwt第二部分data
                        .setIssuedAt(new Date())                   //设置token的生成时间
                        .setId(staff.getStaffId())                    //设置用户id为token id
                        .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))//设置token过期时间。一天
                        .signWith(SignatureAlgorithm.HS256, "fasdfkjnior342f3")
                        .compact();//设置加密方式及密码

                return new ResultVO(ResultCode.LOGIN_SUCCESS_ADMIN,token, staff);
            }
        }
        return new ResultVO(ResultCode.LOGIN_FAILED, null);
    }

    @Override
    public ResultVO staffsRegist(String sId, String pwd) {
        synchronized (this) {
            //1.查询账号是否已经被注册
            Staffs staff = staffsMapper.selectByPrimaryKey(sId);
            //2.如果没有被注册则进行保存
            if (staff == null) {
                try {
                    String md5pwd = MyMD5Util.getEncryptedPwd(pwd);
                    Staffs staff1 = new Staffs();
                    staff1.setStaffId(sId);
                    staff1.setStaffPassword(md5pwd);
                    int i = staffsMapper.insert(staff1);
                    System.out.println(i);
                    if (i > 0) {
                        return new ResultVO(ResultCode.REGIST_SUCCESS, staff1);
                    } else {
                        //return new ResultVO(10002, "注册失败", null);
                        return new ResultVO(ResultCode.REGIST_FAILED_Admin, null);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            //System.out.println("23123");
            return new ResultVO(ResultCode.REGIST_FAILED, null);
        }
    }

    @Override
    public ResultVO addStaffs(Staffs staff) {
        System.out.println(staff);
        Staffs staff1 = staffsMapper.selectByPrimaryKey(staff.getStaffId());
        if (staff1 == null) {
            try {
                String md5pwd = MyMD5Util.getEncryptedPwd(staff.getStaffPassword());
                staff.setStaffPassword(md5pwd);
//                System.out.println(staff);
                int insert = staffsMapper.insert(staff);
                if (insert == 1) {
                    ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "添加员工信息成功",staff);
                    return resultVO;
                }
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }
        return new ResultVO(ResultCode.FAILED, "添加失败,改员工账号已存在", null);
    }

    @Override
    public ResultVO AllStaffs() {
        List<Staffs> staffs = staffsMapper.selectAll();
        if (staffs.size()>0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查看所有员工信息成功",staffs);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "查看所有员工信息失败", null);
    }

    @Override
    public ResultVO findStaffById(String sId) {
        Staffs staffs = staffsMapper.selectByPrimaryKey(sId);
        if (staffs!=null) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查看员工信息成功",staffs);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "查看员工信息失败", null);
    }

    @Override
    public ResultVO updateStaffById(Staffs staff) {
        int i = staffsMapper.updateByPrimaryKeySelective(staff);
        if (i > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "更新员工信息成功",staff);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "更新员工信息失败", null);
    }

    @Override
    public ResultVO deleteStaffById(String sId) {
        int i = staffsMapper.deleteByPrimaryKey(sId);
        if (i > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "删除员工信息成功",null);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "删除员工信息失败", null);
    }


}
